home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / SlideShow.java < prev    next >
Text File  |  1998-09-21  |  17KB  |  596 lines

  1. package symantec.itools.multimedia;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.awt.Container;
  7. import java.net.MalformedURLException;
  8. import java.util.Vector;
  9. import java.awt.Image;
  10. import java.net.URL;
  11. import java.beans.PropertyVetoException;
  12. import java.beans.PropertyChangeListener;
  13. import java.beans.VetoableChangeListener;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.AWTEvent;
  17. import java.awt.AWTEventMulticaster;
  18. import java.util.ResourceBundle;
  19. import java.text.MessageFormat;
  20.  
  21. //    05/30/97    RKM    Made it compile with some of the 1.1 component changes (added catches)
  22. //    06/02/97    LAB    Updated to Java 1.1.  !!! LAB !!! This class should be rewritten as a bean!
  23. //    08/04/97    LAB    Updated version to 1.1. Make sourceActionEvent protected.  Deprecated preferredSize
  24. //                    and added getPreferredSize.  Added getMinimumSize which returns results from
  25. //                    getPreferredSize. Made lightwieght.  Removed update, and repaint.  Made some private
  26. //                    members protected.  Deprecated display().
  27.  
  28. /**
  29.  * This is a basic graphical image "slide show" component. Displays a series of images
  30.  * with descriptive text.
  31.  * @version 1.1, August 1, 1997
  32.  * @author Symantec
  33.  */
  34. public class SlideShow extends Container
  35. {
  36.     /**
  37.      * Constructs a new SlideShow.
  38.      */
  39.     public SlideShow()
  40.     {
  41.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  42.  
  43.         imageViewer = new ImageViewer();
  44.         try
  45.         {
  46.             imageViewer.setStyle(ImageViewer.IMAGE_CENTERED);
  47.         }
  48.         catch (PropertyVetoException exc) {}
  49.  
  50.         imageIndex = 0;
  51.         setLayout(null);
  52.  
  53.         //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  54.         isReshapeHack = false;
  55.     }
  56.  
  57.     /**
  58.      * Return the number of images in the slide show set.
  59.      */
  60.     public int getNumberOfImages()
  61.     {
  62.         if(urlList == null)
  63.             return 0;
  64.  
  65.         return urlList.size();
  66.     }
  67.  
  68.     /**
  69.      * Return the current image index being displayed.
  70.      */
  71.     public int getCurrentImageIndex()
  72.     {
  73.         return imageIndex;
  74.     }
  75.  
  76.     /**
  77.      * Return the URL of the image at the given index.
  78.      * @param index index of image to retrieve URL of
  79.      * @return URL - URL of image at given index
  80.      */
  81.     public URL getURL(int index)
  82.     {
  83.         if(urlList == null)
  84.             return null;
  85.         return (URL)urlList.elementAt(index);
  86.     }
  87.  
  88.     /**
  89.      * Set the description of the image at the given index.
  90.      * @param index index of description to set
  91.      * @param str description string
  92.      * @see #getDescription
  93.      *
  94.      * @exception PropertyVetoException
  95.      * if the specified property value is unacceptable
  96.      */
  97.     public void setDescription(int index, String str) throws PropertyVetoException
  98.     {
  99.         String oldValue = getDescription(index);
  100.  
  101.         vetos.fireVetoableChange("Description", oldValue, str);
  102.  
  103.         descriptions.setElementAt(str, index);
  104.  
  105.         changes.firePropertyChange("Description", oldValue, str);
  106.     }
  107.  
  108.     /**
  109.      * Return the description of the image at the given index.
  110.      * @param index index of image to retrieve description of
  111.      * @return String - description of image at given index
  112.      * @see #setDescription
  113.      */
  114.     public String getDescription(int index)
  115.     {
  116.         return (String)descriptions.elementAt(index);
  117.     }
  118.  
  119.     /**
  120.      * Query if displaying first image in slide show set.
  121.      * @return boolean - true if displaying first image; false
  122.      * otherwise
  123.      * @see #isAtLastImage
  124.      */
  125.     public boolean isAtFirstImage()
  126.     {
  127.         return imageIndex == 0;
  128.     }
  129.  
  130.     /**
  131.      * Query if displaying last image in slide show set.
  132.      * @return boolean - true if displaying last image; false
  133.      * otherwise
  134.      * @see #isAtFirstImage
  135.      */
  136.     public boolean isAtLastImage()
  137.     {
  138.         return imageIndex == (urlList.size() - 1);
  139.     }
  140.  
  141.     /**
  142.      * Display the image at the given index.
  143.      * @param index index of the image to display
  144.      * @param str description string
  145.      * @return int - image index being displayed
  146.      */
  147.     public int setImage(int index)
  148.     {
  149.         if (index >= 0 && index < urlList.size())
  150.         {
  151.             imageIndex = index;
  152.             try
  153.             {
  154.                 imageViewer.setImage((Image)images.elementAt(imageIndex));
  155.             }
  156.             catch(java.beans.PropertyVetoException veto) {}
  157.  
  158.             repaint();
  159.         }
  160.         return imageIndex;
  161.     }
  162.  
  163.     /**
  164.      * Reset slide show and add first image.
  165.      *
  166.      * @param url url of first slide image, if null slide show is reinitialized
  167.      */
  168.     public void setFirstImage(URL url)
  169.     {
  170.         if (url == null)
  171.         {
  172.             try
  173.             {
  174.                 imageViewer.setImage(null);
  175.             }
  176.             catch(java.beans.PropertyVetoException veto) {}
  177.             imageIndex = 0;
  178.             urlList = new Vector();
  179.             images = new Vector();
  180.             descriptions = new Vector();
  181.         }
  182.         else
  183.         {
  184.             if (urlList.size() != 0)
  185.             {
  186.                 urlList.setElementAt(url, 0);
  187.                 descriptions.setElementAt("", 0);
  188.                 images.setElementAt(loadImageFromURL(url), 0);
  189.                 setImage(0);
  190.             }
  191.             else
  192.             {
  193.                 addImageAndDescription(url, "");
  194.             }
  195.         }
  196.     }
  197.  
  198.     /**
  199.      * Reset slide show and add first image.
  200.      *
  201.      * @param image The Image of first slide image, if null slide show is reinitialized
  202.      */
  203.     public void setFirst(Image image)
  204.     {
  205.         if (image == null)
  206.         {
  207.             try
  208.             {
  209.                 imageViewer.setImage(null);
  210.             }
  211.             catch(java.beans.PropertyVetoException veto) {}
  212.             imageIndex = 0;
  213.             urlList = new Vector();
  214.             images = new Vector();
  215.             descriptions = new Vector();
  216.         }
  217.         else
  218.         {
  219.             if (urlList.size() != 0)
  220.             {
  221.                 try
  222.                 {
  223.                     urlList.setElementAt(new URL("file:///noURL"), 0);
  224.                 }
  225.                 catch (MalformedURLException exc)
  226.                 {
  227.                     System.out.println(exc);
  228.                 }
  229.                 descriptions.setElementAt("", 0);
  230.                 images.setElementAt(image, 0);
  231.                 setImage(0);
  232.             }
  233.             else
  234.             {
  235.                 addImageWithDescription(image, "");
  236.             }
  237.         }
  238.     }
  239.  
  240.     /**
  241.      * @deprecated
  242.      */
  243.     //!!! LAB !!! This should go away.
  244.     public void display()
  245.     {
  246.         imageIndex = 0;
  247.  
  248.         setImage(imageIndex);
  249.         add(imageViewer);
  250.         repaint();
  251.         validate();
  252.     }
  253.  
  254.    /**
  255.      * Add an image URL and associated description to the slide
  256.      * show image set.
  257.      * @param url URL of image file
  258.      * @param description description of image
  259.      * @return int - index of added image in slide show set
  260.      */
  261.     public int addImageAndDescription(URL url, String description)
  262.     {
  263.         urlList.addElement(url);
  264.         descriptions.addElement(description);
  265.         images.addElement(loadImageFromURL(url));
  266.  
  267.         int index = urlList.size() - 1;
  268.         if (index == 0)
  269.         {
  270.             imageIndex = 0;
  271.  
  272.             setImage(imageIndex);
  273.             add(imageViewer);
  274.             repaint();
  275.             validate();
  276.         }
  277.  
  278.         return index;
  279.     }
  280.  
  281.    /**
  282.      * Add an Image and associated description to the slide
  283.      * show image set.
  284.      * @param image the java.awt.Image to use
  285.      * @param description description of image
  286.      * @return int - index of added image in slide show set
  287.      */
  288.     public int addImageWithDescription(Image image, String description)
  289.     {
  290.         try
  291.         {
  292.             urlList.addElement(new URL("file:///noURL"));
  293.         }
  294.         catch (MalformedURLException exc)
  295.         {
  296.             System.out.println(exc);
  297.         }
  298.         descriptions.addElement(description);
  299.         images.addElement(image);
  300.  
  301.         int index = urlList.size() - 1;
  302.         if (index == 0)
  303.         {
  304.             imageIndex = 0;
  305.  
  306.             setImage(imageIndex);
  307.             add(imageViewer);
  308.             repaint();
  309.             validate();
  310.         }
  311.  
  312.         return index;
  313.     }
  314.  
  315.     /**
  316.      * Display the next image in the slide show set.
  317.      * Fires an ActionEvent with the actionCommand of "nextImage" to it's listeners.
  318.      * @see #previousImage
  319.      */
  320.     public int nextImage()
  321.     {
  322.         if (! isAtLastImage())
  323.         {
  324.             int rv;
  325.             ++imageIndex;
  326.             rv = setImage(imageIndex);
  327.             sourceActionEvent("nextImage");
  328.             return rv;
  329.         }
  330.         return imageIndex;
  331.     }
  332.  
  333.     /**
  334.      * Display the previous image in the slide show set.
  335.      * Fires an ActionEvent with the actionCommand of "previousImage" to it's listeners.
  336.      * @see #nextImage
  337.      */
  338.     public int previousImage()
  339.     {
  340.         if (! isAtFirstImage())
  341.         {
  342.             int rv;
  343.             --imageIndex;
  344.             rv = setImage(imageIndex);
  345.             sourceActionEvent("previousImage");
  346.             return rv;
  347.         }
  348.         return imageIndex;
  349.     }
  350.  
  351.     /**
  352.      * Returns the recommended dimensions to properly display this component.
  353.      * This is a standard Java AWT method which gets called to determine
  354.      * the recommended size of this component.
  355.      *
  356.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  357.      *          If an image has been loaded, the height and width of the image
  358.      *          is returned.
  359.      * @see #getMinimumSize
  360.      */
  361.     public Dimension getPreferredSize()
  362.     {
  363.         Dimension d = new Dimension(10, 10);
  364.         Dimension imDim;
  365.  
  366.         if(images != null && imageViewer != null)
  367.         {
  368.             //Save the current image
  369.             Image oldImage = imageViewer.getImage();
  370.             for(int i = 0; i < images.size(); i++)
  371.             {
  372.                 if(images.elementAt(i) != null)
  373.                 {
  374.                     try
  375.                     {
  376.                         imageViewer.setImage((Image)images.elementAt(i));
  377.                     }
  378.                     catch(java.beans.PropertyVetoException veto) {}
  379.  
  380.                     imDim = imageViewer.getPreferredSize();
  381.                     d.width = Math.max(imDim.width, d.width);
  382.                     d.height = Math.max(imDim.height, d.height);
  383.                 }
  384.             }
  385.             //Restore the old Image
  386.             try
  387.             {
  388.                 imageViewer.setImage(oldImage);
  389.             }
  390.             catch(java.beans.PropertyVetoException veto) {}
  391.         }
  392.  
  393.         return d;
  394.     }
  395.  
  396.     /**
  397.      * @deprecated
  398.      * @see #getPreferredSize
  399.      */
  400.     public Dimension preferredSize()
  401.     {
  402.         return getPreferredSize();
  403.     }
  404.  
  405.     /**
  406.      * Returns the minimum dimensions to properly display this component.
  407.      * This is a standard Java AWT method which gets called to determine
  408.      * the minimum size of this component.
  409.      *
  410.      * @return  the results from getPreferredSize()
  411.      * @see #getPreferredSize
  412.      */
  413.     public Dimension getMinimumSize()
  414.     {
  415.         return getPreferredSize();
  416.     }
  417.  
  418.     /**
  419.      * Reshapes the Component to the specified bounding box.
  420.      * @param x the x coordinate
  421.      * @param y the y coordinate
  422.      * @param width the width of the component
  423.      * @param height the height of the component
  424.      * @see java.awt.Component#getBounds
  425.      * @see java.awt.Component#setLocation
  426.      * @see java.awt.Component#setSize
  427.      */
  428.     public void setBounds(int x, int y, int width, int height)
  429.     {
  430.         //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  431.         isReshapeHack = true;
  432.         super.setBounds(x, y, width, height);
  433.         imageViewer.setBounds(0, 0, width, height);
  434.         isReshapeHack = false;
  435.     }
  436.  
  437.     /**
  438.      * @deprecated
  439.      * @see #setBounds
  440.      */
  441.     public void reshape(int x, int y, int width, int height)
  442.     {
  443.         //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  444.         super.reshape(x, y, width, height);
  445.         if(! isReshapeHack)
  446.             imageViewer.setBounds(0, 0, width, height);
  447.     }
  448.  
  449.     /**
  450.      * Adds the specified action listener to receive action events
  451.      * from this button.
  452.      * @param l the action listener
  453.      */
  454.     public void addActionListener(ActionListener l)
  455.     {
  456.         actionListener = AWTEventMulticaster.add(actionListener, l);
  457.     }
  458.  
  459.     /**
  460.      * Removes the specified action listener so it no longer receives
  461.      * action events from this button.
  462.      * @param l the action listener
  463.      */
  464.     public void removeActionListener(ActionListener l)
  465.     {
  466.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  467.     }
  468.  
  469.  
  470.     /**
  471.      * Adds a listener for all event changes.
  472.      * @param PropertyChangeListener listener the listener to add.
  473.      * @see #removePropertyChangeListener
  474.      */
  475.     public void addPropertyChangeListener(PropertyChangeListener listener)
  476.     {
  477.         changes.addPropertyChangeListener(listener);
  478.     }
  479.  
  480.     /**
  481.      * Removes a listener for all event changes.
  482.      * @param PropertyChangeListener listener the listener to remove.
  483.      * @see #addPropertyChangeListener
  484.      */
  485.     public void removePropertyChangeListener(PropertyChangeListener listener)
  486.     {
  487.         changes.removePropertyChangeListener(listener);
  488.     }
  489.  
  490.     /**
  491.      * Adds a vetoable listener for all event changes.
  492.      * @param VetoableChangeListener listener the listener to add.
  493.      * @see #removeVetoableChangeListener
  494.      */
  495.     public void addVetoableChangeListener(VetoableChangeListener listener)
  496.     {
  497.         vetos.addVetoableChangeListener(listener);
  498.     }
  499.  
  500.     /**
  501.      * Removes a vetoable listener for all event changes.
  502.      * @param VetoableChangeListener listener the listener to remove.
  503.      * @see #addVetoableChangeListener
  504.      */
  505.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  506.     {
  507.         vetos.removeVetoableChangeListener(listener);
  508.     }
  509.  
  510.     java.awt.event.ActionListener actionListener = null;
  511.  
  512.     /**
  513.      * Fire an action event to the listeners
  514.      * @param actionCommand the command name associated with the ActionEvent to fire.
  515.      */
  516.     protected void sourceActionEvent(String actionCommand)
  517.     {
  518.         if (actionListener != null)
  519.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
  520.     }
  521.  
  522.     /**
  523.      * Loads an image from a given URL
  524.      * System.err's the exception if there was a problem loading the Image.
  525.      * @param url the url referencing the image to load
  526.      * @return the loaded image
  527.      */
  528.     protected Image loadImageFromURL(URL url)
  529.     {
  530.         Image image = null;
  531.  
  532.         try
  533.         {
  534.             image = getToolkit().getImage(url);
  535.         }
  536.         catch (Exception e)
  537.         {
  538.             //throw new MalformedURLException("Error loading image");
  539.             Object[] args = { url };
  540.             System.err.println("Error in SlideShow: " +
  541.                 MessageFormat.format(errors.getString("ErrorLoadingImageForURL"), args));
  542.         }
  543.  
  544.         return image;
  545.     }
  546.  
  547.  
  548.     //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  549.     /**
  550.      * Internal utility flag.
  551.      */
  552.     protected boolean isReshapeHack;
  553.  
  554.     /**
  555.      * The zero-relative index of the currently displayed image.
  556.      * @see #getCurrentImageIndex
  557.      * @see #setImage
  558.      * @see #nextImage
  559.      * @see #previousImage
  560.      */
  561.     protected int imageIndex;
  562.     /**
  563.      * The sub-component that displays the images.
  564.      */
  565.     protected ImageViewer imageViewer;
  566.  
  567.     /**
  568.      * The URLs of the images to display.
  569.      * @see #getURL
  570.      * @see #setFirstImage
  571.      * @see #addImageAndDescription
  572.      */
  573.     protected Vector urlList     = new Vector();
  574.     /**
  575.      * The displayed images, loaded from the image URL list.
  576.      * @see #setFirstImage
  577.      * @see #addImageAndDescription
  578.      */
  579.     protected Vector images      = new Vector();
  580.     /**
  581.      * The displayed image descriptions.
  582.      * @see #getDescription
  583.      * @see #setDescription
  584.      * @see #addImageAndDescription
  585.      */
  586.     protected Vector descriptions = new Vector();
  587.  
  588.     /**
  589.      * Error strings.
  590.      */
  591.     transient protected ResourceBundle errors;
  592.  
  593.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  594.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  595. }
  596.